package yl.hs.bmipfull.service.flow.impl;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import yl.hs.bmipfull.daos.flow.IFlowNodeDao;
import yl.hs.bmipfull.pojo.flow.FlowConditionEntity;
import yl.hs.bmipfull.pojo.flow.FlowNodeEntity;
import yl.hs.bmipfull.service.flow.IFlowConditionService;
import yl.hs.bmipfull.service.flow.IFlowNodeService;
import yl.hs.bmipfull.service.system.impl.SystemBaseService;
import yl.hs.bmipfull.utils.DataState;
import yl.hs.bmipfull.utils.Tl;
import yl.hs.bmipfull.utils.datastate.ActionType;
import yl.hs.bmipfull.viewmodels.PageQuery;
import yl.hs.bmipfull.viewmodels.PageResult;

import java.util.List;
import java.util.Map;

/**
 * 作用:  流程节点信息服务层接口实现<br/>
 * 创建时间: 2021年06月22日 16时24分33秒 <br/>
 *
 * @author 张凯
 */
@Service
public class FlowNodeServiceImpl extends SystemBaseService implements IFlowNodeService {

    @Autowired
    IFlowNodeDao dao;

    @Autowired
    IFlowConditionService conditionService;

    @Override
    public List<FlowNodeEntity> queryList(PageQuery pageQuery) throws Exception {
        pageQuery.checkSortSides(FlowNodeEntity.class);
        return dao.queryPage(pageQuery.getQuery());
    }

    @Override
    public PageResult<FlowNodeEntity> queryPage(PageQuery pageQuery, int rowNum, int page) throws Exception {
        pageQuery.checkSortSides(FlowNodeEntity.class);
        pageQuery.getQuery().put("querySkip", (page - 1) * rowNum);
        pageQuery.getQuery().put("pageSize", rowNum);
        PageResult<FlowNodeEntity> result = new PageResult(rowNum);
        result.setRecords(dao.queryCountByCondition(pageQuery.getQuery()));
        result.setPage(page);
        result.setData(dao.queryPage(pageQuery.getQuery()));
        return result;
    }


    @Override
    public FlowNodeEntity findBySn(String sn) {
        return dao.findBySN(sn);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer saveFlowNode(Map<String, Object> model, String sn) throws Exception {
        Integer result = 0;
        FlowNodeEntity entity = null;
        if (Tl.isEmpty(sn)) {
            //执行保存
            entity = new FlowNodeEntity();
            Tl.getChangeLog(entity, model);
            entity.setPYFieldValue();
            entity.setSn(queryNewSN(FlowNodeEntity.class));
            result = dao.insertNew(entity);
            if (result > 0) {
                writeLog(ActionType.CREATE, entity.getSn(), "插入流程节点信息信息", Tl.toJson(entity), FlowNodeEntity.class);
            }

        } else {
            //执行修改
            entity = findBySn(sn);
            Tl.getChangeLog(entity, model);
            entity.setPYFieldValue();
            result = dao.update(entity);
            if (result > 0) {
                writeLog(ActionType.UPDATE, sn, "更新流程节点信息信息", Tl.toJson(entity), FlowNodeEntity.class);
            }
        }

        return result;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer delete(String[] sn) {
        Integer result = dao.changeStateBySN(sn, DataState.DELETED.code);
        if (result > 0) {
            writeLog(ActionType.DELETE, sn, FlowNodeEntity.class);
        }
        return result;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer saveFlowNode(List<FlowNodeEntity> nodeList) throws Exception {
        Integer result = 0;
        for (FlowNodeEntity flowNodeEntity : nodeList) {
            result += saveFlowNode(flowNodeEntity);
        }
        return result;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer saveFlowNode(FlowNodeEntity nodeEntity) throws Exception {
        nodeEntity.setSn(queryNewSN(FlowNodeEntity.class));
        nodeEntity.setPYFieldValue();
        return dao.insertNew(nodeEntity);
    }

    @Override
    public FlowNodeEntity findTimerNodeByFlowInfoSn(String flowInfoSn) {
        List<FlowNodeEntity> flowNodeList = dao.queryNodeByFlowSn(flowInfoSn);
        for (FlowNodeEntity flowNode : flowNodeList) {
            FlowConditionEntity conditionEntity = conditionService.findByRightNodeSn(flowNode.getSn());
            if (conditionEntity == null) {
                return flowNode;
            }
        }
        return null;
    }

    @Override
    public FlowNodeEntity findNextNode(FlowNodeEntity nowNode) {
        FlowConditionEntity conditionByLeft = conditionService.findByLeftNodeSn(nowNode.getSn());
        return findBySn(conditionByLeft.getRightNodeSn());
    }


}